//
//  HYLoginApi.m
//  TJHYHardwareProduct
//
//  Created by JianWei Chen on 2018/1/23.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYLoginApi.h"

@implementation HYLoginApi{
    NSDictionary *_dic;
}

-(id)initWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        _dic = dic;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"/user/login";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPOST;
}

- (id)requestArgument {
    NSMutableDictionary *dic = [NSMutableDictionary getPubdicGuest];
    [dic setValuesForKeysWithDictionary:_dic];
    if (_dic[@"username"]) {
        NSString* base64Text = [NSString encryptUseAES:_dic[@"username"] key:nil];
        [dic setObject:base64Text forKey:@"username"];
    }
    if (_dic[@"password"]) {
        NSString* base64Text = [NSString encryptUseAES:_dic[@"password"] key:nil];
        [dic setObject:base64Text forKey:@"password"];
    }
    NSString *sign=[NSString getSign:dic];
    [dic setObject:sign forKey:@"sign"];
    return dic;
}


-(void)dealloc
{
    NSLog(@"%@ dealloc",[self class]);
}

+ (void)login:(NSString *)username password:(NSString *)password success:(void (^)(NSDictionary *))successBack fail:(void (^)(NSDictionary *))failBack
{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setValue:username forKey:@"username"];
    [requestDic setValue:password forKey:@"password"];
    
    HYLoginApi *api = [[HYLoginApi alloc]initWithDic:requestDic];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        if (successBack) {
            successBack(request.responseObject);
        }
    } failure:^(YTKBaseRequest * _Nonnull request) {
        if (failBack) {
            failBack(request.responseObject);
        }
    }];
}
@end
